package com.yuju.rpt.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.exception.BusinessException;
import com.yuju.rpt.dao.ICheckinCustomTypeDetailDao;
import com.yuju.rpt.dao.entity.CheckinCustomTypeDetail;
import com.yuju.rpt.model.CheckinCustomTypeDetailModel;
import com.yuju.rpt.model.CheckinCustomTypeModel;
import com.yuju.rpt.service.ICheckinCustomTypeDetailService;
import com.yuju.rpt.service.ICheckinCustomTypeService;
import com.yuju.util.Utils;

/**
 * 类名：CheckinCustomTypeDetailServiceImpl  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2018年2月5日 下午5:41:25  <br />
 * @version : 1.1 <br />
 */
@Service
public class CheckinCustomTypeDetailServiceImpl extends AbstractBaseService<CheckinCustomTypeDetailModel, CheckinCustomTypeDetail, String>
		implements ICheckinCustomTypeDetailService {
	
	@Autowired
	private ICheckinCustomTypeDetailDao checkinCustomTypeDetailDao;
	
	@Autowired
	private ICheckinCustomTypeService checkinCustomTypeService;
	
	@Override
	public IBaseDao<CheckinCustomTypeDetailModel, CheckinCustomTypeDetail, String> getDao() {
		return checkinCustomTypeDetailDao;
	}

	/**
	 * 功能:获得入住分析详情（按客源） <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2018年2月5日 下午5:45:58<br/>
	 * 
	 * @param checkinCustomTypeDetailModel
	 * @return <br/>
	 * @see com.yuju.rpt.service.ICheckinCustomTypeDetailService#getCheckinStatDetailByCustomType(com.yuju.rpt.model.CheckinCustomTypeDetailModel)
	 */
	@Override
	public List<CheckinCustomTypeDetailModel> getCheckinStatDetailByCustomType(
			CheckinCustomTypeDetailModel checkinCustomTypeDetailModel) {
		return checkinCustomTypeDetailDao.getCheckinStatDetailByCustomType(checkinCustomTypeDetailModel);
	}

	/**
	 * 功能: 查找总计 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2018年2月6日 下午3:24:28<br/>
	 * 
	 * @param checkinCustomTypeDetailModel
	 * @return <br/>
	 * @see com.yuju.rpt.service.ICheckinCustomTypeDetailService#getSum(com.yuju.rpt.model.CheckinCustomTypeDetailModel)
	 */
	@Override
	public CheckinCustomTypeDetailModel getSum(CheckinCustomTypeModel checkinCustomTypeModel) {
		CheckinCustomTypeModel model = checkinCustomTypeService.find(checkinCustomTypeModel);
		if(Utils.isNull(model)){
			throw new BusinessException(ReturnCodeEnum.NO_DATA.getCode(), ReturnCodeEnum.NO_DATA.getDesc());
		}
		CheckinCustomTypeDetailModel checkinCustomTypeDetailModel = new CheckinCustomTypeDetailModel();
		checkinCustomTypeDetailModel.setParentId(model.getId());
		CheckinCustomTypeDetailModel sum = this.getSum(checkinCustomTypeDetailModel);
		if(model.getKfRoomSum() == 0){
			sum.setKfRateZf(new BigDecimal(0));
		}else{
			sum.setKfRateZf(sum.getKfSaleSum().divide(new BigDecimal(model.getKfRoomSum()), 4, BigDecimal.ROUND_HALF_UP));
		}
		if(sum.getKfSaleSum().compareTo(new BigDecimal("0"))==0 || Utils.isNull(sum.getKfSaleSum())){
			sum.setRate(new BigDecimal(0));
		}else{
			sum.setRate(sum.getKfSaleSum()
					.divide(sum.getKfSaleSum(), 4, BigDecimal.ROUND_HALF_UP));
		}
		if(sum.getKfSaleSum().compareTo(new BigDecimal("0"))==0 || Utils.isNull(sum.getKfSaleSum())){
			sum.setKfAverage(new BigDecimal(0));
		}else{
			sum.setKfAverage(sum.getYsFfSum()
					.divide(sum.getKfSaleSum(), 2, BigDecimal.ROUND_HALF_UP));
		}
		return sum;
	}

	@Override
	public CheckinCustomTypeDetailModel getSum(CheckinCustomTypeDetailModel checkinCustomTypeDetailModel) {
		return checkinCustomTypeDetailDao.getSum(checkinCustomTypeDetailModel);
	}

}
